<?php

/**
 * ECSHOP 支付接口函数库
 * ============================================================================
 * * 版权所有 2008-2015 秦皇岛商之翼网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.68ecshop.com;
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和
 * 使用；不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * $Author: derek $
 * $Id: lib_payment.php 17218 2011-01-24 04:10:41Z derek $
 */

if (!defined('IN_ECS'))
{
    die('Hacking attempt');
}

/**
 * 取得返回信息地址
 * @param   string  $code   支付方式代码
 */
function return_url($code)
{
    return $GLOBALS['ecs']->url() . 'respond.php?code=' . $code;
}

/**
 *  取得某支付方式信息
 *  @param  string  $code   支付方式代码
 */
function get_payment($code)
{
    $sql = 'SELECT * FROM ' . $GLOBALS['ecs']->table('payment').
           " WHERE pay_code = '$code' AND enabled = '1'";
    $payment = $GLOBALS['db']->getRow($sql);

    if ($payment)
    {
        $config_list = unserialize($payment['pay_config']);

        foreach ($config_list AS $config)
        {
            $payment[$config['name']] = $config['value'];
        }
    }

    return $payment;
}

/**
 *  通过订单sn取得订单ID
 *  @param  string  $order_sn   订单sn
 *  @param  blob    $voucher    是否为会员充值
 */
function get_order_id_by_sn($order_sn, $voucher = 'false')
{
    if ($voucher == 'true')
    {
        if(is_numeric($order_sn))
        {
              return $GLOBALS['db']->getOne("SELECT log_id FROM " . $GLOBALS['ecs']->table('pay_log') . " WHERE order_id=" . $order_sn . ' AND order_type=1');
        }
        else
        {
            return "";
        }
    }
    else
    {
        if(is_numeric($order_sn))
        {
            $sql = 'SELECT order_id FROM ' . $GLOBALS['ecs']->table('order_info'). " WHERE order_sn = '$order_sn'";
            $order_id = $GLOBALS['db']->getOne($sql);
        }
        if (!empty($order_id))
        {
            $pay_log_id = $GLOBALS['db']->getOne("SELECT log_id FROM " . $GLOBALS['ecs']->table('pay_log') . " WHERE order_id='" . $order_id . "'");
            return $pay_log_id;
        }
        else
        {
            return "";
        }
    }
}

/**
 *  通过订单ID取得订单商品名称
 *  @param  string  $order_id   订单ID
 */
function get_goods_name_by_id($order_id)
{
    $sql = 'SELECT goods_name FROM ' . $GLOBALS['ecs']->table('order_goods'). " WHERE order_id = '$order_id'";
    $goods_name = $GLOBALS['db']->getCol($sql);
    return implode(',', $goods_name);
}

/**
 * 检查支付的金额是否与订单相符
 *
 * @access  public
 * @param   string   $log_id      支付编号
 * @param   float    $money       支付接口返回的金额
 * @return  true
 */
function check_money($log_id, $money)
{
    if(is_numeric($log_id))
    {
        $sql = 'SELECT order_amount FROM ' . $GLOBALS['ecs']->table('pay_log') .
              " WHERE log_id = '$log_id'";
        $amount = $GLOBALS['db']->getOne($sql);
    }
    else
    {
        return false;
    }
    if ($money == $amount)
    {
        return true;
    }
    else
    {
        return false;
    }
}

//byking九层分销
function tijiao_getmoney($user_id,$get_money,$info,$kouchu_money,$shixiao_time,$rdr_user_id){
    $sql_fcget = 'INSERT INTO ' .$GLOBALS['ecs']->table('fenchen_get').' (user_id,add_time,money,info,kouchu_money,zcr_user_id,shixiao_time)'.
        " VALUES ('$user_id',  '".gmtime()."','$get_money','$info','$kouchu_money','$rdr_user_id','$shixiao_time')";
    $res=$GLOBALS['db']->query($sql_fcget);
    return $res;
}
function wuji_check($parent_id){
    
   $sqltj = "SELECT user_id FROM " . $GLOBALS['ecs']->table('users') . " where parent_id='$parent_id' and user_rank=2";
   $info_record_count = $GLOBALS['db']->getAll($sqltj);
   $record_count=count($info_record_count);

   // $sqltj = "SELECT user_id FROM " . $GLOBALS['ecs']->table('users') . " where parent_id='$parent_id'";
   // $info_record_count = $GLOBALS['db']->getAll($sqltj);
    
    //$sumrs=0;
   // foreach ($info_record_count as $key =>$val){
       // $sqlrd = "SELECT user_id FROM " . $GLOBALS['ecs']->table('user_account') . " WHERE user_id='$val[user_id]' and is_rudan=1 and is_paid=1 and type_id=1";
        //$rowrd = $GLOBALS['db']->getAll($sqlrd);
        //if($rowrd){
           // $sumrs +=1;
       // }
        
   // }
    //$record_count=$sumrs;
    
    return $record_count;
}
function jiandianjiang_check($user_id){
    $sqljc = "SELECT user_id FROM " . $GLOBALS['ecs']->table('fenchen_get') . " where user_id='$user_id' and type=1";
    $infojc = $GLOBALS['db']->getRow($sqljc);
    return $infojc;
    
}
function charujiandianjian($user_id,$jdj_money,$rdr_user_id,$end_hour){
    $sql_fcget1 = 'INSERT INTO ' .$GLOBALS['ecs']->table('fenchen_get').' (user_id,add_time,money,info,zcr_user_id,type,shixiao_time)'.
        " VALUES ('$user_id',  '".gmtime()."','$jdj_money','见点奖奖励','$rdr_user_id',1,'$end_hour')";
    $res=$GLOBALS['db']->query($sql_fcget1);
    if($res){
        $res=1;
    }else{
        $res=0;
    }
    return $res;
}
//byking九层分销 end
/**
 * 修改订单的支付状态
 *
 * @access  public
 * @param   string  $log_id     支付编号
 * @param   integer $pay_status 状态
 * @param   string  $note       备注
 * @return  void
 */
function order_paid($log_id, $pay_status = PS_PAYED, $note = '')
{
   
    /* 取得支付编号 */
    $log_id = intval($log_id);
    if ($log_id > 0)
    {
        /* 取得要修改的支付记录信息 */
        $sql = "SELECT * FROM " . $GLOBALS['ecs']->table('pay_log') .
                " WHERE log_id = '$log_id'";
        $pay_log = $GLOBALS['db']->getRow($sql);
        if ($pay_log && $pay_log['is_paid'] == 0)
        {
            /* 修改此次支付操作的状态为已付款 */
            $sql = 'UPDATE ' . $GLOBALS['ecs']->table('pay_log') .
                    " SET is_paid = '1' WHERE log_id = '$log_id'";
            $GLOBALS['db']->query($sql);

            /* 根据记录类型做相应处理 */
            if ($pay_log['order_type'] == PAY_ORDER)
            {
                /* 取得订单信息 */
                $sql = 'SELECT order_id, user_id,supplier_id, order_sn, consignee, address, tel, shipping_id, extension_code, extension_id, goods_amount ' .
                        'FROM ' . $GLOBALS['ecs']->table('order_info') .
                       " WHERE order_id = '$pay_log[order_id]' OR parent_order_id = '$pay_log[order_id]' ";
                $orderinfo    = $GLOBALS['db']->getAll($sql);
		foreach($orderinfo as $key => $order)
		{
	                $order_id = $order['order_id'];
	                $order_sn = $order['order_sn'];
			$suppid = $order['supplier_id'];
			$supplier[$suppid]	 = $order_sn;
	
	                /* 修改订单状态为已付款 */
	                $sql = 'UPDATE ' . $GLOBALS['ecs']->table('order_info') .
	                            " SET order_status = '" . OS_CONFIRMED . "', " .
	                                " confirm_time = '" . gmtime() . "', " .
	                                " pay_status = '$pay_status', " .
	                                " pay_time = '".gmtime()."', " .
	                                " money_paid = order_amount," .
	                                " order_amount = 0 ".
	                       "WHERE order_id = '$order_id'";
	                $GLOBALS['db']->query($sql);
	
	                /* 记录订单操作记录 */
	                order_action($order_sn, OS_CONFIRMED, SS_UNSHIPPED, $pay_status, $note, $GLOBALS['_LANG']['buyer']);
	
	               /* 如果需要，发短信 */  //jx
	               include_once('send.php');
	
					send_sms($supplier,'客户已付款，订单号为：ordersn请注意查看。【shopname】',2);
	
	                /* 对虚拟商品的支持 */
	                $virtual_goods = get_virtual_goods($order_id);
	                if (!empty($virtual_goods))
	                {
	                    $msg = '';
	                    if (!virtual_goods_ship($virtual_goods, $msg, $order_sn, true))
	                    {
	                        $GLOBALS['_LANG']['pay_success'] .= '<div style="color:red;">'.$msg.'</div>'.$GLOBALS['_LANG']['virtual_goods_ship_fail'];
	                    }
	
	                    /* 如果订单没有配送方式，自动完成发货操作 */
	                    if ($order['shipping_id'] == -1)
	                    {
	                        /* 将订单标识为已发货状态，并记录发货记录 */
	                        $sql = 'UPDATE ' . $GLOBALS['ecs']->table('order_info') .
	                               " SET shipping_status = '" . SS_SHIPPED . "', shipping_time = '" . gmtime() . "'" .
	                               " WHERE order_id = '$order_id'";
	                        $GLOBALS['db']->query($sql);
	
	                         /* 记录订单操作记录 */
	                        order_action($order_sn, OS_CONFIRMED, SS_SHIPPED, $pay_status, $note, $GLOBALS['_LANG']['buyer']);
	                        $integral = integral_to_give($order);
	                        log_account_change($order['user_id'], 0, 0, intval($integral['rank_points']), intval($integral['custom_points']), sprintf($GLOBALS['_LANG']['order_gift_integral'], $order['order_sn']));
	                    }
	                }
               }

            }
            elseif ($pay_log['order_type'] == PAY_SURPLUS)
            {
                $sql = 'SELECT `id` FROM ' . $GLOBALS['ecs']->table('user_account') .  " WHERE `id` = '$pay_log[order_id]' AND `is_paid` = 1  LIMIT 1";
                $res_id=$GLOBALS['db']->getOne($sql);
                if(empty($res_id))
                {
                    /* 更新会员预付款的到款状态 */
                    $sql = 'UPDATE ' . $GLOBALS['ecs']->table('user_account') .
                           " SET paid_time = '" .gmtime(). "', is_paid = 1" .
                           " WHERE id = '$pay_log[order_id]' LIMIT 1";
                    $GLOBALS['db']->query($sql);

                    /* 取得添加预付款的用户以及金额 */
                    $sql = "SELECT user_id, amount,is_rudan,type_id FROM " . $GLOBALS['ecs']->table('user_account') .
                            " WHERE id = '$pay_log[order_id]'";
                    $arr = $GLOBALS['db']->getRow($sql);

                    /* 修改会员帐户金额 */
                    $_LANG = array();
                    include_once(ROOT_PATH . 'languages/' . $GLOBALS['_CFG']['lang'] . '/user.php');
                    if($arr['is_rudan']<1){
                    log_account_change($arr['user_id'], $arr['amount'], 0, 0, 0, $_LANG['surplus_type_0'], ACT_SAVING);
                    }else{
                        $sqlsz="SELECT * FROM ". $GLOBALS['ecs']->table('fenchen_sz') ."";
                        $infosz=$GLOBALS['db']->getRow($sqlsz);
                        if($arr['type_id']>1){
                            log_account_change($arr['user_id'], $arr['amount'], 0, 0, 0, $_LANG['surplus_type_0'], ACT_SAVING);
                        }else{
                            log_account_change($arr['user_id'], 0, 0, 0, $infosz['total_money'], "套餐支付赠送消费积分", 99);
                            $sqlvip = 'UPDATE ' . $GLOBALS['ecs']->table('users') .
                            " SET user_rank =2" .
                            " WHERE user_id = '$arr[user_id]' LIMIT 1";
                            $GLOBALS['db']->query($sqlvip);
                            
                            $zhuceyhqq=100;
                            $sqlzzqqsz = "UPDATE " . $GLOBALS['ecs']->table('users') .
                            " SET qiquan = '".$zhuceyhqq. "'" .
                            " WHERE user_id ='".$arr[user_id]. "'";
                            $GLOBALS['db']->query($sqlzzqqsz);
                        }
                        //byking九层分销
                        $sql="SELECT * FROM ". $GLOBALS['ecs']->table('fenchen_money') ."";
                        $info=$GLOBALS['db']->getAll($sql);               
                        $rdr_user_id=$arr['user_id'];
                        $sql = "SELECT user_id,parent_id,user_name FROM " . $GLOBALS['ecs']->table('users') . " WHERE user_id='$rdr_user_id' LIMIT 1";
                        $row = $GLOBALS['db']->getRow($sql);
                        if($arr['type_id']>1){
                            $infosz['jdj_money']=0;
                            $infosz['peixun_money']=0; 
                            $zhifuwenzhi="购物充值";
                        }else{
                            $zhifuwenzhi="入单";
                        }
                        require_once (ROOT_PATH . 'sms/sms.php');
                        if($row['parent_id']){
                            $sql_1 = "SELECT user_id,parent_id,user_rank,mobile_phone FROM " . $GLOBALS['ecs']->table('users') . " WHERE user_id='$row[parent_id]' LIMIT 1";
                            $row_1 = $GLOBALS['db']->getRow($sql_1);
                            $get_money_1=($infosz['total_money']-$infosz['peixun_money']-$infosz['jdj_money'])*$info[0]['money_baifenbi']/100;
                            if($arr['type_id']>1){
                                $get_money_1=$arr['amount']*0.01;
                            }
                            
                            if($row_1['user_rank']==2){
                                $content="您获得邀请补助红包".intval($get_money_1)."元,红包时效为24小时。";
                                sendSMS($row_1['mobile_phone'], $content);//byking 发送短信
                                $shixiao_time=24;
                            }else{
                                $content="您获赠VIP后，可获得邀请补助红包".intval($get_money_1)."元,红包时效为72小时。";                             
                                sendSMS($row_1['mobile_phone'], $content);//byking 发送短信
                                $shixiao_time=72;
                            }
                            
                            $one_1=tijiao_getmoney($row_1['user_id'],intval($get_money_1),"推荐".$row['user_name'].$zhifuwenzhi."一级推荐人奖励",$infosz['peixun_money']+$infosz['jdj_money'],$shixiao_time,$rdr_user_id);
                            //见点奖
                            $tongjixj=wuji_check($row_1['user_id']);
                            if($tongjixj>=6 && $arr['type_id']==1){                       
                                    $resulust=charujiandianjian($row_1['user_id'],$infosz['jdj_money'],$rdr_user_id,$infosz['end_hour']);
                                    $contentjdj="您获得见点奖红包".$infosz['jdj_money']."元,红包时效为".$infosz['end_hour']."小时。";
                                    sendSMS($row_1['mobile_phone'], $contentjdj);
                               
                            }
                            //见点奖 end
                            if($row_1['parent_id']){
                                $sql_2 = "SELECT user_id,parent_id,user_rank,mobile_phone FROM " . $GLOBALS['ecs']->table('users') . " WHERE user_id='$row_1[parent_id]' LIMIT 1";
                                $row_2 = $GLOBALS['db']->getRow($sql_2);
                                $get_money_2=($infosz['total_money']-$infosz['peixun_money']-$infosz['jdj_money'])*$info[1]['money_baifenbi']/100;
                                if($arr['type_id']>1){
                                    $get_money_2=$arr['amount']*0.01;
                                }
                                if($row_2['user_rank']==2){
                                    $content="您获得邀请补助红包".intval($get_money_2)."元,红包时效为24小时。";
                                    sendSMS($row_2['mobile_phone'], $content);//byking 发送短信
                                    $shixiao_time=24;
                                }else{
                                    $content="您获赠VIP后，可获得邀请补助红包".intval($get_money_2)."元,红包时效为72小时。";
                                    sendSMS($row_2['mobile_phone'], $content);//byking 发送短信
                                    $shixiao_time=72;
                                }
                                
                                $one_2=tijiao_getmoney($row_2['user_id'],intval($get_money_2),"推荐".$row['user_name'].$zhifuwenzhi."二级推荐人奖励",$infosz['peixun_money']+$infosz['jdj_money'],$shixiao_time,$rdr_user_id);
                                
                                //见点奖
                                $tongjixj=wuji_check($row_2['user_id']);
                                if($tongjixj>=5 && $arr['type_id']==1){
                                    if($resulust!=1){
                                        $resulust=charujiandianjian($row_2['user_id'],$infosz['jdj_money'],$rdr_user_id,$infosz['end_hour']);
                                        $contentjdj="您获得见点奖红包".$infosz['jdj_money']."元,红包时效为".$infosz['end_hour']."小时。";
                                        sendSMS($row_2['mobile_phone'], $contentjdj);
                                     }
                                
                                }
                                //见点奖 end
                                
                            }
                            if($row_2['parent_id']){
                                $sql_3 = "SELECT user_id,parent_id,user_rank,mobile_phone FROM " . $GLOBALS['ecs']->table('users') . " WHERE user_id='$row_2[parent_id]' LIMIT 1";
                                $row_3 = $GLOBALS['db']->getRow($sql_3);
                                $get_money_3=($infosz['total_money']-$infosz['peixun_money']-$infosz['jdj_money'])*$info[2]['money_baifenbi']/100;
                                if($arr['type_id']>1){
                                    $get_money_3=$arr['amount']*0.01;
                                }
                                if($row_3['user_rank']==2){
                                    $content="您获得邀请补助红包".intval($get_money_3)."元,红包时效为24小时。";
                                    sendSMS($row_3['mobile_phone'], $content);//byking 发送短信
                                    $shixiao_time=24;
                                }else{
                                    $content="您获赠VIP后，可获得邀请补助红包".intval($get_money_3)."元,红包时效为72小时。";
                                    sendSMS($row_3['mobile_phone'], $content);//byking 发送短信
                                    $shixiao_time=72;
                                }
                                $one_3=tijiao_getmoney($row_3['user_id'],intval($get_money_3),"推荐".$row['user_name'].$zhifuwenzhi."三级推荐人奖励",$infosz['peixun_money']+$infosz['jdj_money'],$shixiao_time,$rdr_user_id);
                                //见点奖
                                $tongjixj=wuji_check($row_3['user_id']);
                                if($tongjixj>=5 && $arr['type_id']==1){
                                    if($resulust!=1){
                                        $resulust=charujiandianjian($row_3['user_id'],$infosz['jdj_money'],$rdr_user_id,$infosz['end_hour']);
                                        $contentjdj="您获得见点奖红包".$infosz['jdj_money']."元,红包时效为".$infosz['end_hour']."小时。";
                                        sendSMS($row_3['mobile_phone'], $contentjdj);
                                    }
                                }
                                //见点奖 end
                            }
                            
                            if($row_3['parent_id']){
                                $sql_4 = "SELECT user_id,parent_id,user_rank,mobile_phone FROM " . $GLOBALS['ecs']->table('users') . " WHERE user_id='$row_3[parent_id]' LIMIT 1";
                                $row_4 = $GLOBALS['db']->getRow($sql_4);
                                $get_money_4=($infosz['total_money']-$infosz['peixun_money']-$infosz['jdj_money'])*$info[3]['money_baifenbi']/100;
                                if($arr['type_id']>1){
                                    $get_money_4=$arr['amount']*0.01;
                                }
                                if($row_4['user_rank']==2){
                                    $content="您获得邀请补助红包".intval($get_money_4)."元,红包时效为24小时。";
                                    sendSMS($row_4['mobile_phone'], $content);//byking 发送短信
                                    $shixiao_time=24;
                                }else{
                                    $content="您获赠VIP后，可获得邀请补助红包".intval($get_money_4)."元,红包时效为72小时。";
                                    sendSMS($row_4['mobile_phone'], $content);//byking 发送短信
                                    $shixiao_time=72;
                                }
                                $one_4=tijiao_getmoney($row_4['user_id'],intval($get_money_4),"推荐".$row['user_name'].$zhifuwenzhi."四级推荐人奖励",$infosz['peixun_money']+$infosz['jdj_money'],$shixiao_time,$rdr_user_id);
                                //见点奖
                                $tongjixj=wuji_check($row_4['user_id']);
                                if($tongjixj>=5 && $arr['type_id']==1){
                                    if($resulust!=1){
                                        $resulust=charujiandianjian($row_4['user_id'],$infosz['jdj_money'],$rdr_user_id,$infosz['end_hour']);
                                        $contentjdj="您获得见点奖红包".$infosz['jdj_money']."元,红包时效为".$infosz['end_hour']."小时。";
                                        sendSMS($row_4['mobile_phone'], $contentjdj);
                                    }
                                }
                                //见点奖 end
                            }
                            
                            if($row_4['parent_id']){
                                $sql_5 = "SELECT user_id,parent_id,user_rank,mobile_phone FROM " . $GLOBALS['ecs']->table('users') . " WHERE user_id='$row_4[parent_id]' LIMIT 1";
                                $row_5 = $GLOBALS['db']->getRow($sql_5);
                                $get_money_5=($infosz['total_money']-$infosz['peixun_money']-$infosz['jdj_money'])*$info[4]['money_baifenbi']/100;
                                if($arr['type_id']>1){
                                    $get_money_5=$arr['amount']*0.01;
                                }
                                if($row_5['user_rank']==2){
                                    $content="您获得邀请补助红包".intval($get_money_5)."元,红包时效为24小时。";
                                    sendSMS($row_5['mobile_phone'], $content);//byking 发送短信
                                    $shixiao_time=24;
                                }else{
                                    $content="您获赠VIP后，可获得邀请补助红包".intval($get_money_5)."元,红包时效为72小时。";
                                    sendSMS($row_5['mobile_phone'], $content);//byking 发送短信
                                    $shixiao_time=72;
                                }
                                $one_5=tijiao_getmoney($row_5['user_id'],intval($get_money_5),"推荐".$row['user_name'].$zhifuwenzhi."五级推荐人奖励",$infosz['peixun_money']+$infosz['jdj_money'],$shixiao_time,$rdr_user_id);
                                //见点奖
                                $tongjixj=wuji_check($row_5['user_id']);
                                if($tongjixj>=5 && $arr['type_id']==1){
                                    if($resulust!=1){
                                        $resulust=charujiandianjian($row_5['user_id'],$infosz['jdj_money'],$rdr_user_id,$infosz['end_hour']);
                                        $contentjdj="您获得见点奖红包".$infosz['jdj_money']."元,红包时效为".$infosz['end_hour']."小时。";
                                        sendSMS($row_5['mobile_phone'], $contentjdj);
                                    }
                                }
                                //见点奖 end
                            }
                            
                            if($row_5['parent_id']){
                                $sql_6 = "SELECT user_id,parent_id,user_rank,mobile_phone FROM " . $GLOBALS['ecs']->table('users') . " WHERE user_id='$row_5[parent_id]' LIMIT 1";
                                $row_6 = $GLOBALS['db']->getRow($sql_6);
                                $get_money_6=($infosz['total_money']-$infosz['peixun_money']-$infosz['jdj_money'])*$info[5]['money_baifenbi']/100;
                                if($arr['type_id']>1){
                                    $get_money_6=$arr['amount']*0.01;
                                }
                                if($row_6['user_rank']==2){
                                    $content="您获得邀请补助红包".intval($get_money_6)."元,红包时效为24小时。";
                                    sendSMS($row_6['mobile_phone'], $content);//byking 发送短信
                                    $shixiao_time=24;
                                }else{
                                    $content="您获赠VIP后，可获得邀请补助红包".intval($get_money_6)."元,红包时效为72小时。";
                                    sendSMS($row_6['mobile_phone'], $content);//byking 发送短信
                                    $shixiao_time=72;
                                }
                                $one_6=tijiao_getmoney($row_6['user_id'],intval($get_money_6),"推荐".$row['user_name'].$zhifuwenzhi."六级推荐人奖励",$infosz['peixun_money']+$infosz['jdj_money'],$shixiao_time,$rdr_user_id);
                                //见点奖
                                $tongjixj=wuji_check($row_6['user_id']);
                                if($tongjixj>=5 && $arr['type_id']==1){
                                    if($resulust!=1){
                                        $resulust=charujiandianjian($row_6['user_id'],$infosz['jdj_money'],$rdr_user_id,$infosz['end_hour']);
                                        $contentjdj="您获得见点奖红包".$infosz['jdj_money']."元,红包时效为".$infosz['end_hour']."小时。";
                                        sendSMS($row_6['mobile_phone'], $contentjdj);
                                    }
                                }
                                //见点奖 end
                            }
                            
                            if($row_6['parent_id']){
                                $sql_7 = "SELECT user_id,parent_id,user_rank,mobile_phone FROM " . $GLOBALS['ecs']->table('users') . " WHERE user_id='$row_6[parent_id]' LIMIT 1";
                                $row_7 = $GLOBALS['db']->getRow($sql_7);
                                $get_money_7=($infosz['total_money']-$infosz['peixun_money']-$infosz['jdj_money'])*$info[6]['money_baifenbi']/100;
                                if($arr['type_id']>1){
                                    $get_money_7=$arr['amount']*0.01;
                                }
                                if($row_7['user_rank']==2){
                                    $content="您获得邀请补助红包".intval($get_money_7)."元,红包时效为24小时。";
                                    sendSMS($row_7['mobile_phone'], $content);//byking 发送短信
                                    $shixiao_time=24;
                                }else{
                                    $content="您获赠VIP后，可获得邀请补助红包".intval($get_money_7)."元,红包时效为72小时。";
                                    sendSMS($row_7['mobile_phone'], $content);//byking 发送短信
                                    $shixiao_time=72;
                                }
                                $one_7=tijiao_getmoney($row_7['user_id'],intval($get_money_7),"推荐".$row['user_name'].$zhifuwenzhi."七级推荐人奖励",$infosz['peixun_money']+$infosz['jdj_money'],$shixiao_time,$rdr_user_id);
                                //见点奖
                                $tongjixj=wuji_check($row_7['user_id']);
                                if($tongjixj>=5 && $arr['type_id']==1){
                                    if($resulust!=1){
                                        $resulust=charujiandianjian($row_7['user_id'],$infosz['jdj_money'],$rdr_user_id,$infosz['end_hour']);
                                        $contentjdj="您获得见点奖红包".$infosz['jdj_money']."元,红包时效为".$infosz['end_hour']."小时。";
                                        sendSMS($row_7['mobile_phone'], $contentjdj);
                                    }
                                }
                                //见点奖 end
                            }
                            
                            if($row_7['parent_id']){
                                $sql_8 = "SELECT user_id,parent_id,user_rank,mobile_phone FROM " . $GLOBALS['ecs']->table('users') . " WHERE user_id='$row_7[parent_id]' LIMIT 1";
                                $row_8 = $GLOBALS['db']->getRow($sql_8);
                                $get_money_8=($infosz['total_money']-$infosz['peixun_money']-$infosz['jdj_money'])*$info[7]['money_baifenbi']/100;
                                if($arr['type_id']>1){
                                    $get_money_8=$arr['amount']*0.01;
                                }
                                if($row_8['user_rank']==2){
                                    $content="您获得邀请补助红包".intval($get_money_8)."元,红包时效为24小时。";
                                    sendSMS($row_8['mobile_phone'], $content);//byking 发送短信
                                    $shixiao_time=24;
                                }else{
                                    $content="您获赠VIP后，可获得邀请补助红包".intval($get_money_8)."元,红包时效为72小时。";
                                    sendSMS($row_8['mobile_phone'], $content);//byking 发送短信
                                    $shixiao_time=72;
                                }
                                
                                $one_8=tijiao_getmoney($row_8['user_id'],intval($get_money_8),"推荐".$row['user_name'].$zhifuwenzhi."八级推荐人奖励",$infosz['peixun_money']+$infosz['jdj_money'],$shixiao_time,$rdr_user_id);
                                //见点奖
                                $tongjixj=wuji_check($row_8['user_id']);
                                if($tongjixj>=5 && $arr['type_id']==1){
                                    if($resulust!=1){
                                        $resulust=charujiandianjian($row_8['user_id'],$infosz['jdj_money'],$rdr_user_id,$infosz['end_hour']);
                                        $contentjdj="您获得见点奖红包".$infosz['jdj_money']."元,红包时效为".$infosz['end_hour']."小时。";
                                        sendSMS($row_8['mobile_phone'], $contentjdj);
                                    }
                                }
                                //见点奖 end
                            }
                            
                            if($row_8['parent_id']){
                                $sql_9 = "SELECT user_id,parent_id,user_rank,mobile_phone FROM " . $GLOBALS['ecs']->table('users') . " WHERE user_id='$row_8[parent_id]' LIMIT 1";
                                $row_9 = $GLOBALS['db']->getRow($sql_9);
                                $get_money_9=($infosz['total_money']-$infosz['peixun_money']-$infosz['jdj_money'])*$info[8]['money_baifenbi']/100;
                                if($arr['type_id']>1){
                                    $get_money_9=$arr['amount']*0.01;
                                }
                                if($row_9['user_rank']==2){
                                    $content="您获得邀请补助红包".intval($get_money_9)."元,红包时效为24小时。";
                                    sendSMS($row_9['mobile_phone'], $content);//byking 发送短信
                                    $shixiao_time=24;
                                }else{
                                    $content="您获赠VIP后，可获得邀请补助红包".intval($get_money_9)."元,红包时效为72小时。";
                                    sendSMS($row_9['mobile_phone'], $content);//byking 发送短信
                                    $shixiao_time=72;
                                }
                                $one_9=tijiao_getmoney($row_9['user_id'],intval($get_money_9),"推荐".$row['user_name'].$zhifuwenzhi."九级推荐人奖励",$infosz['peixun_money']+$infosz['jdj_money'],$shixiao_time,$rdr_user_id);
                                //见点奖
                                $tongjixj=wuji_check($row_9['user_id']);
                                if($tongjixj>=5 && $arr['type_id']==1){
                                    if($resulust!=1){
                                        charujiandianjian($row_9['user_id'],$infosz['jdj_money'],$rdr_user_id,$infosz['end_hour']);
                                        $contentjdj="您获得见点奖红包".$infosz['jdj_money']."元,红包时效为".$infosz['end_hour']."小时。";
                                        sendSMS($row_9['mobile_phone'], $contentjdj);
                                    }
                                }
                                //见点奖 end
                            }
                        }
                        
                        if($arr['type_id']>1){
                            $infosz['total_money']=0;
                            $type_val=1;
                        }
                        
                        $caiwu_money=$infosz['total_money']-$infosz['peixun_money']-$infosz['jdj_money']-$get_money_1-$get_money_2-$get_money_3-$get_money_4-$get_money_5-$get_money_6-$get_money_7-$get_money_8-$get_money_9;
                        $sql_caiwu= 'INSERT INTO ' .$GLOBALS['ecs']->table('pt_caiwu').' (user_id,add_time,caiwu_money,type)'.
                            " VALUES ('$rdr_user_id',  '".gmtime()."','$caiwu_money','$type_val')";
                        $GLOBALS['db']->query($sql_caiwu);
                        //byking九层分销 end
                    }
                }
            }
        }
        else
        {
            /* 取得已发货的虚拟商品信息 */
            $post_virtual_goods = get_virtual_goods($pay_log['order_id'], true);

            /* 有已发货的虚拟商品 */
            if (!empty($post_virtual_goods))
            {
                $msg = '';
                /* 检查两次刷新时间有无超过12小时 */
                $sql = 'SELECT pay_time, order_sn FROM ' . $GLOBALS['ecs']->table('order_info') . " WHERE order_id = '$pay_log[order_id]'";
                $row = $GLOBALS['db']->getRow($sql);
                $intval_time = gmtime() - $row['pay_time'];
                if ($intval_time >= 0 && $intval_time < 3600 * 12)
                {
                    $virtual_card = array();
                    foreach ($post_virtual_goods as $code => $goods_list)
                    {
                        /* 只处理虚拟卡 */
                        if ($code == 'virtual_card')
                        {
                            foreach ($goods_list as $goods)
                            {
                                if ($info = virtual_card_result($row['order_sn'], $goods))
                                {
                                    $virtual_card[] = array('goods_id'=>$goods['goods_id'], 'goods_name'=>$goods['goods_name'], 'info'=>$info);
                                }
                            }

                            $GLOBALS['smarty']->assign('virtual_card',      $virtual_card);
                        }
                    }
                }
                else
                {
                    $msg = '<div>' .  $GLOBALS['_LANG']['please_view_order_detail'] . '</div>';
                }

                $GLOBALS['_LANG']['pay_success'] .= $msg;
            }

           /* 取得未发货虚拟商品 */
           $virtual_goods = get_virtual_goods($pay_log['order_id'], false);
           if (!empty($virtual_goods))
           {
               $GLOBALS['_LANG']['pay_success'] .= '<br />' . $GLOBALS['_LANG']['virtual_goods_ship_fail'];
           }
        }
    }
}

?>